package com.jr.controller;

import com.jr.pojo.ProjectInfo;
import com.jr.service.IProjectInfoService;
import com.jr.service.impl.ProjectInfoServiceImpl;
import com.sun.org.apache.bcel.internal.generic.FSUB;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
@WebServlet("/projectinfo/*")
public class Test_0827 extends HttpServlet {
    private IProjectInfoService ips =new ProjectInfoServiceImpl();
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String uri = request.getRequestURI();
        int index = uri.lastIndexOf("/")+1;
        String pName = uri.substring(index);
        if (pName.length()==0){
            pName = "showdt";
        }
        Class<Test_0827> testClass = Test_0827.class;
        Method method = null;
        try {
            method = testClass.getMethod(pName,HttpServletRequest.class,HttpServletResponse.class);
            request.setCharacterEncoding("utf-8");
            response.setContentType("test/html;charset=utf-8");
            method.invoke(this,request,response);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }
    public void showdt(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        ArrayList<ProjectInfo> projectInfos = ips.queryByStatus(-1);
        HttpSession session = request.getSession();
        session.setAttribute("list",projectInfos);
        request.getRequestDispatcher("/index3.jsp").forward(request,response);
    }
    public void query(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
//        int pid = Integer.parseInt(request.getParameter("pid"));
        int status = Integer.parseInt(request.getParameter("status"));
        ArrayList<ProjectInfo> projectInfo = ips.queryByStatus(status);
        HttpSession session = request.getSession();
        session.setAttribute("list",projectInfo);
        System.out.println("rffffffffffssss"+projectInfo);
        request.getRequestDispatcher("/index3.jsp").forward(request,response);
    }
    public void queryById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        int pid = Integer.parseInt(request.getParameter("pid"));
//        int status = Integer.parseInt(request.getParameter("status"));
        ProjectInfo projectInfo = ips.queryByPid(pid);
        HttpSession session = request.getSession();
        session.setAttribute("pro",projectInfo);
        response.sendRedirect("/project/update3.jsp");
    }
    public void updatePro3(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        int pid = Integer.parseInt(request.getParameter("pid"));
        int status = Integer.parseInt(request.getParameter("status"));

        ProjectInfo pro = new ProjectInfo();
        pro.setPid(pid);
        pro.setStatus(status);

        boolean b = ips.updateProject(pro);
        HttpSession session = request.getSession();
        session.setAttribute("mess",b?"ok!":"error!");

        ArrayList<ProjectInfo> projectInfos = ips.queryByStatus(-1);
        session.setAttribute("list",projectInfos);
        response.sendRedirect("/project/index3.jsp");
    }
}
